package com.hlz.fastnote

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_detail_note.*
import org.litepal.LitePal

class DetailNoteActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail_note)

        intent?.apply {

            val note = getParcelableExtra<Note>("Note")?.let {note ->
                tvName.text = note.title
                tvContent.text = note.content

                ivBack.setOnClickListener {
                    finish()
                }

                tvShare.setOnClickListener {
                    shareText(this@DetailNoteActivity,note.content)
                }

                tvCopy.setOnClickListener {
                    Utils.copy(note.content)
                    ToastUtil.showToast("复制成功！")
                }

                ivDelete.setOnClickListener {
                    val dialog = DeleteDialog(this@DetailNoteActivity)
                    dialog.show()
                    dialog.setOnDialogListener(object :DeleteDialog.OnDialogListener{
                        override fun sure() {
                            LitePal.delete(Note::class.java,note.id)
                            ToastUtil.showToast("删除成功！")
                            finish()
                        }
                    })

                }
            }



        }



    }


    fun shareText(context: Context, text: String?) {
        if (TextUtils.isEmpty(text)) {
            ToastUtil.showToast("未获取到分享内容！")
            return
        }
        val textIntent = Intent(Intent.ACTION_SEND)
        textIntent.type = "text/plain"
        textIntent.putExtra(Intent.EXTRA_TEXT, text)
        context.startActivity(
            Intent.createChooser(
                textIntent,
                "分享"
            )
        )
    }
}